package xeroxware.framework.view;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.servlet.view.AbstractView;

/**
 * <pre>
 * ImageView
 * - Image를 출력한다.
 * </pre>
 * 
 * @since 2013. 2. 21.
 * @author XEROXER
 */
public class ImageView extends AbstractView {

	private static final Logger logger = LoggerFactory.getLogger(ImageView.class);

	@Override
	protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		File file = (File) model.get("file");
		InputStream inStream = null;
		OutputStream outStream = response.getOutputStream();

		response.setContentType(getContentType());
		response.setContentLength((int) file.length());

		try {
			inStream = new FileInputStream(file);

			FileCopyUtils.copy(inStream, outStream);
		} catch (Exception e) {
			logger.error("[{}] {}", e.getClass().getSimpleName(), e.getMessage(), e);
		} finally {
			inStream.close();
		}

		outStream.flush();
		outStream.close();
	}
}
